# _*_ coding: utf-8 _*_
#!/usr/bin/env python

#      ___________________
#  ___| Modulbeschreibung \_________________________________________________________________________
# | Spielt Sounds und Musik ab und verwaltet die Soundkanäle
# |__________________________________________________________________| Zuständigkeit [Jannik |____

import threading
import pygame
import sys, os
import time

def addWorkPath(filename):
	"""Aus Portabilitätsgruenden wie relativen Pfadangaben unter verschiedenen Systemen gehandhabt werden. Haengt das Arbeitsverzeichnis vor den übergebenen Dateinamen"""
	return os.path.join(os.path.dirname(sys.argv[0]), 'data', filename)

class Sound(threading.Thread):
	
	def __init__(self):
		
		threading.Thread.__init__(self)
		pygame.mixer.init() 
		pygame.mixer.set_reserved(2)

		
		self.music = {	'itheme':			addWorkPath('music/introtheme.ogg'),
						'theme':			addWorkPath('music/theme.ogg'),
						'startrace':		addWorkPath('music/startrace.ogg'),
						'imariocircuit':	addWorkPath('music/intromariocircuit.ogg'),
						'mariocircuit':		addWorkPath('music/mariocircuit.ogg'),
						'gameover':			addWorkPath('music/gameover.ogg'),
						'immunity':			addWorkPath('music/immunity.ogg'),
						'rankedout':		addWorkPath('music/rankedout.ogg'),
						'endrace':			addWorkPath('music/cupwins.ogg')
						}
		self.sounds = {	'click':	pygame.mixer.Sound(addWorkPath('sounds/click.ogg')),
						'click2':	pygame.mixer.Sound(addWorkPath('sounds/click2.ogg')),
						'crash':	pygame.mixer.Sound(addWorkPath('sounds/crash.ogg')),
						}
						
	def playMusic(self, music,  intro = None, loop = -1):
		
		pygame.mixer.music.load(self.music[music])
		pygame.mixer.music.play(loop)
	
	def playSound(self, sound):
		
		self.sounds[sound].play()


if __name__ == '__main__': 
	sound = Sound()
	sound.playMusic("theme", "itheme")
	time.sleep(1)
	sound.playSound("crash")
	time.sleep(40)
	sound.playMusic("immunity", "itheme")
	time.sleep(20)
